//
// DepRepoFile
//
// Copyright (C) jextra.net.
//
//  This file is part of the net.jextra.dep Library.
//
//  The net.jextra.dep Library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2.1 of the License, or (at your option) any later version.
//
//  The net.jextra.dep Library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with the net.jextra.dep Library; if not, write to the Free
//  Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
//  02111-1307 USA.
//

package net.jextra.dep.repo;

/**
 * Represents a single fie in the repository.
 */
public class DepRepoFile implements Comparable<DepRepoFile>
{
    // ============================================================
    // Enums
    // ============================================================

    public enum Type
    {
        file( "f" ),
        directory( "d" );

        private String tag;

        Type( String tag )
        {
            this.tag = tag;
        }

        public static Type getTagType( String tag )
        {
            for ( Type type : values() )
            {
                if ( type.tag().equals( tag ) )
                {
                    return type;
                }
            }

            return null;
        }

        public String tag()
        {
            return tag;
        }

        @Override
        public String toString()
        {
            return tag();
        }
    }

    // ============================================================
    // Fields
    // ============================================================

    private DepRepo repo;
    private String componentName;
    private String versionNumber;
    private String path;
    private Type type;
    private long timestamp;
    private long size;

    // ============================================================
    // Constructors
    // ============================================================

    public DepRepoFile( DepRepo repo, String componentName, String versionNumber, String path, Type type,
        long timestamp, long size )
    {
        assert repo != null;
        assert componentName != null;
        assert versionNumber != null;
        assert path != null;
        assert type != null;

        this.repo = repo;
        this.componentName = componentName;
        this.versionNumber = versionNumber;
        this.path = path;
        this.type = type;
        this.timestamp = timestamp;
        this.size = size;
    }

    // ============================================================
    // Methods
    // ============================================================

    // ----------
    // public
    // ----------

    public boolean isFile()
    {
        return getType() == Type.file;
    }

    public boolean isDirectory()
    {
        return getType() == Type.directory;
    }

    public Type getType()
    {
        return type;
    }

    public void setType( Type type )
    {
        this.type = type;
    }

    public String getComponentName()
    {
        return componentName;
    }

    public void setComponentName( String componentName )
    {
        this.componentName = componentName;
    }

    public String getVersionNumber()
    {
        return versionNumber;
    }

    public void setVersionNumber( String versionNumber )
    {
        this.versionNumber = versionNumber;
    }

    public String getPath()
    {
        return path;
    }

    public void setPath( String path )
    {
        this.path = path;
    }

    public long getTimestamp()
    {
        return timestamp;
    }

    public void setTimestamp( long timestamp )
    {
        this.timestamp = timestamp;
    }

    public long getSize()
    {
        return size;
    }

    public void setSize( long size )
    {
        this.size = size;
    }

    @Override
    public boolean equals( Object obj )
    {
        if ( !( obj instanceof DepRepoFile ) )
        {
            return false;
        }

        DepRepoFile other = (DepRepoFile) obj;

        if ( !componentName.equals( other.componentName ) )
        {
            return false;
        }
        else if ( !versionNumber.equals( other.versionNumber ) )
        {
            return false;
        }
        else if ( !path.equals( other.path ) )
        {
            return false;
        }

        return true;
    }

    @Override
    public int compareTo( DepRepoFile other )
    {
        if ( isDirectory() && !other.isDirectory() )
        {
            return -1;
        }
        else if ( !isDirectory() && other.isDirectory() )
        {
            return 1;
        }

        return path.compareTo( other.path );
    }

    @Override
    public String toString()
    {
        try
        {
            return String.format( "%s %s:%s/%s/%s", type, repo.getName(), componentName, versionNumber, path );
        }
        catch ( Exception ex )
        {
            throw new RuntimeException( ex );
        }
    }
}
